<template>
    <el-dialog
        v-model="dialogVisible"
        title="新建租户账号"
        width="30%"
        :before-close="handClose"
    >
        <div>
            <el-form :model="formData" ref="formRef" label-width="80px" :rules="rules">
                <el-form-item label="账号邮箱" prop="username">
                    <el-input 
                        v-model="formData.username" 
                        placeholder="请输入账号邮箱"
                        :prefix-icon="User"
                    />
                </el-form-item>
                <el-form-item label="昵称" prop="nickname">
                    <el-input 
                        v-model="formData.nickname" 
                        placeholder="请输入昵称"
                        :prefix-icon="MoreFilled"
                    />
                </el-form-item>
            </el-form>
        </div>
        <template #footer>
        <span class="dialog-footer">
            <el-button @click="closeDialog">取消</el-button>
            <el-button type="primary" @click="confirmDialog">
            确认
            </el-button>
        </span>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import { ref, onMounted,reactive, computed, defineProps, defineEmits, watch} from 'vue'
import {useRouter, useRoute} from 'vue-router';
import { ElMessage, ElMessageBox } from 'element-plus'
import type { FormInstance, FormRules } from 'element-plus'
import { User, MoreFilled } from '@element-plus/icons-vue'
import TenantApi from '../../apis/tenant'
import {useUserStore} from '../../stores/user'

const router = useRouter();
const route = useRoute();
const userStore = useUserStore()

const formRef = ref<FormInstance>()
const formData = ref({
    username: "",
    nickname: ""
});

const rules = reactive<FormRules>({
    username: [
        { required: true, message: '请输入用户名', trigger: 'blur' },
        { min: 3, max: 32, message: '最小长度3, 最大长度16', trigger: 'blur' },
    ],
    nickname: [
        { required: true, message: '请输入昵称', trigger: 'blur' },
        { min: 3, max: 16, message: '最小长度3, 最大长度16', trigger: 'blur' },
    ],
})

const props = defineProps({
    isShow: Boolean,
    handleUpdated: Function,
})
const emit = defineEmits(['update:isShow']);

const dialogVisible = ref(props.isShow);
watch(()=> props.isShow,() => {
    console.log('props.isShow change', props.isShow);
    dialogVisible.value = props.isShow
});

const handClose = () =>{
    console.log('handClose');
    emit('update:isShow', false)
}

const closeDialog = () => {
    console.log('closeDialog');
    emit('update:isShow', false)
}

const confirmDialog = () => {
    TenantApi.addTenantAccount(formData.value.username, formData.value.nickname).then(ret=>{
        if(ret.code == 0){
            ElMessage({
                type: 'success',
                message: "添加成功",
            });
            if(props.handleUpdated){
                props.handleUpdated();
            }
            setTimeout(async () => {
                emit('update:isShow', false)
            }, 1500);
        }else{
            ElMessage({
                type: 'error',
                message: "添加失败",
            });
        }
    }).catch(err=>{
        ElMessage({
            type: 'error',
            message: err
        });
    });
    
}
</script>

<style scoped>
</style>
